﻿using CodeGen.TS.Generation.Members;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CodeGen.TS.Generation
{
    public static partial class CodeGenerationHelper
    {
        public static IList<TSPropertyInfo> GetTSFields(this ICodeGenerator generator, bool withRecursion)
        {
            List<PropertyInfo> propertyInfos = new List<PropertyInfo>();
            var type = generator.Type;

            while (type != null)
            {

                propertyInfos.InsertRange(0, type.GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly |
              BindingFlags.GetProperty | BindingFlags.SetProperty | BindingFlags.Public));
                if (generator.BaseTypeMode != BaseTypeMode.Include)
                {
                    break;
                }
                type = type.BaseType;
            }

            return GenerateFields(generator, propertyInfos, generator.UseObservable, withRecursion);
        }
        public static IList<TSPropertyInfo> GenerateFields(ICodeGenerator baseGenerator, IEnumerable<PropertyInfo> properties, bool useObservable, bool withRecursion)
        {
            List<TSPropertyInfo> infos = new List<TSPropertyInfo>();
            foreach (var property in properties)
            {
                if (property.PropertyType == typeof(object))//It is likely to work without this as well, but to output object is really too much
                    continue;

                if (property.Name == "Item" && property.GetIndexParameters().Length > 0)
                { //Indexed members will lead to duplicate fields if they are not excluded

                    continue;
                }
                var info = new TSPropertyInfo(property);
                SetScriptType(info, property.PropertyType, withRecursion, useObservable, baseGenerator);
                infos.Add(info);

            }
            return infos;
        }
    }
}
